// components/poster/poster.js
const app = getApp();
var {Api} = require('../../api/api')
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    show: {
      type: Boolean,
      value: false,
      observer: '_setDate'
    },
    er: {
      type: String,
      value: ''
    }
  },
  /**
   * 组件的初始数据
   */
  data: {
    tabid: 0,
    src: '',
    hai:'',//海报
    domain: Api.domain
  },

  /**
   * 组件的方法列表
   */
  methods: {
    noshow() {
      this.setData({
        show: false
      })
    },
    _setDate: function (newDate, oldDate) {
      console.log(newDate, 'newDate', oldDate)
      if (newDate) {
        this.setData({
          hai:wx.$cache.get('hai')
        })
        this.drawCanvas()
      }
    },

    qie(e) {
      let { index } = e.currentTarget.dataset
      this.setData({
        tabid: index
      })
      this.drawCanvas()
    },
    drawCanvas() {//开始画canvas
     
      console.log("wewwewe");

      console.log(this.data.hai);
      var that = this

      var img = this.data.hai[this.data.tabid].img
      //var tou = wx.$cache.get('userinfo').img
      
      //var img = this.data.domain+"rgshare3.png";
      var tou = this.data.domain+"index_menu_4.png";
      

      var er = this.data.er
      let images = []
      let blocks = []
      if (this.data.tabid == 0) {
        images = [ //图一的
          { x: 0, y: 0, width: 652, height: 878, url: img },
          { x: 234, y: 620, width: 185, height: 190, url: er },
          { x: 282, y: 671, width: 88, height: 88, url: tou, borderRadius: 88 },
        ]
        blocks = [
          { x: 219, y: 607, borderRadius: 215, backgroundColor: '#FFFFFF', width: 215, height: 215 }
        ]
      }
      if (this.data.tabid == 1) {
        images = [ //图二的
          { x: 0, y: 0, width: 652, height: 878, url: img },
          { x: 210, y: 562, width: 185, height: 190, url: er },
          { x: 258, y: 612, width: 88, height: 88, url: tou, borderRadius: 88 },
        ]
        blocks = [
          { x: 185, y: 537, backgroundColor: '#FFFFFF', width: 234, height: 236, borderWidth: 4, borderColor: '#41122B' }
        ]
      }
      if (this.data.tabid == 2) {
        images = [ //图三的
          { x: 0, y: 0, width: 652, height: 878, url: img },
          { x: 426, y: 640, width: 185, height: 190, url: er },
          { x: 474, y: 690, width: 88, height: 88, url: tou, borderRadius: 88 },
        ]
        blocks = [
          { x: 410, y: 627, borderRadius: 215, backgroundColor: '#FFFFFF', width: 215, height: 215 }
        ]
      }
      if (this.data.tabid == 3) {
        images = [ //图四的
          // { x: 0, y: 0, width: 652, height: 878, url: img },
          { x: 25, y: 22, width: 296, height: 304, url: er ,zIndex:2},
          { x: 102, y: 102, width: 141, height: 141, url: tou, borderRadius: 141,zIndex:3},
        ]
        blocks = [
          { x: 0, y: 0, borderRadius: 346, backgroundColor: '#FFFFFF', width: 346, height: 346 }
        ]
        this.setData({
          posterConfig: {
            width: 346,
            height: 346,
            images: images,
            // texts:texts,
            blocks: blocks
          }
        }, function () {
          console.log(that.selectComponent('#poster'))
          that.selectComponent('#poster').onCreate(true)
        })
      }

      // let texts = [
      //     {x:90,y:211,fontSize:44,text:title,color: '#343D44',zIndex:2,lineNum:1},
      //     {x:93,y:273,fontSize:38,text:price,color: '#FB525A',zIndex:2,lineNum:1},
      //     {x:93,y:320,fontSize:26,text:jie,color: '#9496A8',zIndex:2,lineNum:1},
      //     {x:341,y:674,fontSize:30,text:tips,color: '#6A7984',zIndex:2,lineNum:2,textAlign:'center',width:328},
      //   ]
     if(this.data.tabid!=3){
      this.setData({
        posterConfig: {
          width: 652,
          height: 878,
          images: images,
          // texts:texts,
          blocks: blocks
        }
      }, function () {
        console.log(that.selectComponent('#poster'))
        that.selectComponent('#poster').onCreate(true)
      })
     }
    },
    onPosterSuccess(e) { //预览海报

      console.log("预览还和好说歹说");

      const {
        detail
      } = e;
      console.log(detail)
      this.setData({
        src: detail,
      })
    },
    //开始绘图包错
    onPosterFail: function (e) {
      console.log(e)
    },
    bindbtn() {//保存图片
      var that = this
      console.log("23232323");
      console.log(this.data.src);
     
      
      wx.saveImageToPhotosAlbum({
        filePath: this.data.src,
        //授权成功，保存图片
        success: function (data) {
          wx.showToast({
            title: '保存成功',
            icon: 'success',
            duration: 2000
          })
        },
        //授权失败
        fail: function (err) {

          console.log(err.errMsg);

          if (err.errMsg) {//重新授权弹框确认
            wx.showModal({
              title: '提示',
              content: '您好,请先授权，在保存此图片。',
              showCancel: false,
              success(res) {
                if (res.confirm) {//重新授权弹框用户点击了确定
                  wx.openSetting({//进入小程序授权设置页面
                    success(settingdata) {
                      console.log(settingdata)
                      if (settingdata.authSetting['scope.writePhotosAlbum']) {//用户打开了保存图片授权开关
                        wx.saveImageToPhotosAlbum({
                          filePath: that.data.src,
                          success: function (data) {
                            wx.showToast({
                              title: '保存成功',
                              icon: 'success',
                              duration: 2000
                            })
                          },
                        })
                      } else {//用户未打开保存图片到相册的授权开关
                        wx.showModal({
                          title: '温馨提示',
                          content: '授权失败，请稍后重新获取',
                          showCancel: false,
                        })
                      }
                    }
                  })
                }
              }
            })
          }
        }
      })
    },
    go(e){
      let {url} = e.currentTarget.dataset
      this.noshow()
      wx.navigateTo({
        url,
      })
    }
  }
})
